package com.woniuxy.wallet.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.Escort;
import com.woniuxy.commons.entity.Order;
import com.woniuxy.commons.entity.Wallet;
import com.woniuxy.commons.util.ResponseResult;
import com.woniuxy.wallet.entity.A;
import com.woniuxy.wallet.service.WalletService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/wallet")
public class WalletController {

    @Resource
    private WalletService walletService;


    @GetMapping("/fistPage/{pageNum}/{handleSize}")
    public ResponseResult fistPage(@PathVariable int pageNum,@PathVariable int handleSize){

        ResponseResult result = walletService.firstPage(pageNum,handleSize);

        return result;
    }

    //查询余额
    @GetMapping("/balance")
    public ResponseResult balance(){

        ResponseResult balance = walletService.balance();

        return balance;
    }

    //查询待结算金额
    @GetMapping("/waitMoney")
    public ResponseResult waitMoney(){

        ResponseResult responseResult = walletService.waitMoney();

        return responseResult;
    }

    //查询收益
    @GetMapping("/shouyi")
    public ResponseResult shouyi(){
        ResponseResult finish = walletService.finish();

        ResponseResult amount = walletService.amount();

        double datafinish = (double)finish.getData();

        double dataamount = (double)amount.getData();

        double v = datafinish - dataamount;

        ResponseResult responseResult = new ResponseResult();

        responseResult.setData(v);

        return responseResult;
    }

    @GetMapping("/findAll/{pageNum}/{handleSize}")
    public ResponseResult findAll(@PathVariable int pageNum,@PathVariable int handleSize){

        PageHelper.startPage(pageNum,handleSize);

        ResponseResult all1 = walletService.findAll1();

        PageHelper.startPage(pageNum,handleSize);

        ResponseResult all2 = walletService.findAll2();

        List<Wallet> data = (List) all1.getData();

        System.out.println(data);
        List<Wallet> data2 = (List) all2.getData();
        System.out.println(data2);

        for (int i = 0; i < data.size(); i++) {
            data.get(i).setBalance(data2.get(i).getBalance());
        }

        PageInfo<Wallet> pageInfo = PageInfo.of(data);

        ResponseResult responseResult = new ResponseResult();

        responseResult.setData(pageInfo);

        return responseResult;
    }

    @GetMapping("/findTime/{ot}")
    public ResponseResult findTime(@PathVariable String ot){
        if(ot.equals("null")){
            ResponseResult all = findAll(1, 5);
            PageInfo<Wallet> data = (PageInfo)all.getData();
            ResponseResult responseResult = new ResponseResult();
            responseResult.setData(data);
            return responseResult;
        }
        PageHelper.startPage(1,1);
        ResponseResult time1 = walletService.findTime1(ot);

        PageHelper.startPage(1,1);
        ResponseResult time2 = walletService.findTime2(ot);

        List<Wallet> data1 = (List)time1.getData();

        List<Wallet> data2 = (List)time2.getData();

        System.out.println(ot);



        if (data1.size() != 0){
            data1.get(0).setBalance(data2.get(0).getBalance());

            System.out.println(data1);

            PageInfo<Wallet> pageInfo = PageInfo.of(data1);

            ResponseResult responseResult = new ResponseResult();

            responseResult.setData(pageInfo);

            return responseResult;
        }

        return null;

    }

    @PostMapping("/filtrate")
    public ResponseResult filtrate(@RequestBody Wallet wallet){

        System.out.println(wallet);

        ResponseResult filtrate = walletService.shaixuan(wallet);

        System.out.println(filtrate);

        return filtrate;
    }

    @GetMapping("/allcs/{pageNum}/{handleSize}")
    public ResponseResult allcs(@PathVariable int pageNum,@PathVariable int handleSize){

        ResponseResult responseResult = walletService.allCS(pageNum,handleSize);

        return responseResult;
    }

    @PostMapping("/sx")
    public ResponseResult sx(@RequestBody A aaa){

        String peihuyuan = aaa.getPeihuyuan();

        if(aaa.getPeihuyuan().equals("")){
            aaa.setPeihu(0);
        }else {
            Escort eByName = walletService.findEByName(peihuyuan);
            if(eByName!=null){
                aaa.setPeihu(eByName.getEid());
            }else {
                aaa.setPeihu(-1);
            }
        }



        ResponseResult t = walletService.findT(aaa);

        List data = (List)t.getData();

        PageInfo<Order> pageInfo = PageInfo.of(data);

        ResponseResult responseResult = new ResponseResult();

        responseResult.setData(pageInfo);

        return responseResult;
    }

    @PostMapping("/ok")
    @Transactional
    public ResponseResult ok(@RequestBody Order order){

        String no = order.getNo();

        walletService.updateStatus(no);

        //1收入 2 支出

        walletService.insertWallet(order,2);

        ResponseResult allcs = allcs(1, 5);


        return allcs;
    }
}
